package com.xbongbong.paas.controller.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.pojo.dto.PaasConfigGetDTO;
import com.xbongbong.pro.pojo.vo.PaasConfigGetVO;
import com.xbongbong.pro.service.toolbox.subform.PaasConfigService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/03/30 15:53
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/config")
public class PaasConfigController {

    @Resource
    private PaasConfigService paasConfigService;

    /**
     * 获取关联产品和子表单的最大行数
     *
     * @param paasConfigGetDTO
     * @param br                  br
     * @return CompanyConfigAddBatchVO
     */
    @RequestMapping(value = "/getSubFormConfig", method = RequestMethod.POST)
    public String get(@RequestBody @Valid PaasConfigGetDTO paasConfigGetDTO, BindingResult br) {
        XbbResponse<PaasConfigGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaasConfigGetVO paasConfigGetVO = paasConfigService.getSubFormConfig(paasConfigGetDTO);
                response = new XbbResponse<>(paasConfigGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
